package com.we823.project.repository.service.repo;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import com.we823.project.model.repo.MaterialStockOutModel;
import com.we823.project.repository.dao.repo.MaterialStockOutRepository;
import com.we823.project.repository.service.AbstractBaseService;

@Service("materialStockOutService")
public class MaterialStockOutService extends
		AbstractBaseService<MaterialStockOutModel> {

	private MaterialStockOutRepository materialStockOutRepository;

	@Autowired
	public MaterialStockOutService(
			MaterialStockOutRepository materialStockOutRepository) {
		super(materialStockOutRepository);
		this.materialStockOutRepository = materialStockOutRepository;
	}

	public List<MaterialStockOutModel> findByIdIn(List<Integer> stockOutIdList) {
		return materialStockOutRepository.findByIdIn(stockOutIdList);
	}

	public Page<MaterialStockOutModel> findByEdittime(int year, int month,
			int day, PageRequest pageRequest) {
		if (pageRequest != null) {
			if (year > 0 && month == 0) {
				return materialStockOutRepository.findByEdittime(year,
						pageRequest);
			}

			if (month > 0 && day == 0) {
				return materialStockOutRepository.findByEdittime(year, month,
						pageRequest);
			}

			if (day > 0) {
				return materialStockOutRepository.findByEdittime(year, month,
						day, pageRequest);
			}
		} else {
			List<MaterialStockOutModel> materialStockOutModels = null;
			if (year > 0 && month == 0) {
				materialStockOutModels = materialStockOutRepository
						.findByEdittime(year);
			}

			if (month > 0 && day == 0) {
				materialStockOutModels = materialStockOutRepository
						.findByEdittime(year, month);
			}

			if (day > 0) {
				materialStockOutModels = materialStockOutRepository
						.findByEdittime(year, month, day);	
			}
			return new PageImpl<MaterialStockOutModel>(materialStockOutModels);
		}
		return null;
	}

	public Page<MaterialStockOutModel> findByEdittimeAndUserid(int year,
			int month, int day, Set<Integer> userIds, PageRequest pageRequest) {
		if (pageRequest != null) {
			if (year > 0 && month == 0) {
				return materialStockOutRepository.findByEdittimeAndUserid(year,userIds,pageRequest);
			}

			if (month > 0 && day == 0) {
				return materialStockOutRepository.findByEdittimeAndUserid(year,month,userIds,pageRequest);
			}

			if (day > 0) {
				return materialStockOutRepository.findByEdittimeAndUserid(year,month,day,userIds,pageRequest);
			}
		} else {
			List<MaterialStockOutModel> materialStockOutModels = null;
			if (year > 0 && month == 0) {
				materialStockOutModels = materialStockOutRepository
						.findByEdittime(year);
			}

			if (month > 0 && day == 0) {
				materialStockOutModels = materialStockOutRepository
						.findByEdittime(year, month);
			}

			if (day > 0) {
				materialStockOutModels = materialStockOutRepository
						.findByEdittime(year, month, day);	
			}
			return new PageImpl<MaterialStockOutModel>(materialStockOutModels);
		}
		return null;
	}

	public List<MaterialStockOutModel> findByFlag(int flag) {
		return materialStockOutRepository.findByFlag(flag);
	}

	public List<MaterialStockOutModel> save(List<MaterialStockOutModel> materialStockOutModels) {
		return materialStockOutRepository.save(materialStockOutModels);
	}

}
